/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ComExWeb.service.interceptors;

import ComExWeb.exception.ServiceException;
import ComExWeb.util.ExceptionUtil;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 
 */
public class CrudServiceInterceptor {

    private static final Logger log = LoggerFactory.getLogger(CrudServiceInterceptor.class);

    @AroundInvoke
    public Object handleException(InvocationContext invocationContext) throws Exception {
        log.trace("### Entrou no interceptor...");

        try {
            return invocationContext.proceed();

        } catch (ServiceException ex) {
            throw ex;
        } catch (Exception e) {
            ExceptionUtil eu = new ExceptionUtil();
            throw eu.throwServiceException(e);

//            if (invocationContext.getTarget().getClass().isAnnotationPresent(HandleServiceException.class)) {
//                throw ExceptionUtil.throwServiceException(e);
//            } else {
//                throw e;
//            }

            /*if (invocationContext.getTarget().getClass().isAnnotationPresent(HandlePersistenceException.class)) {
            throw ExceptionUtil.lancaExcecaoDAO2(e);
            } else if (invocationContext.getTarget().getClass().isAnnotationPresent(HandleServiceException.class)) {
            throw ExceptionUtil.lancaExcecaoService2(e);
            } else {
            throw e;
            }*/
        }
    }
}
